package com.home.homepage.service.prefecture.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eig.common.entity.homepageentity.prefecture.TbZone;
import com.eig.common.vo.Result;
import com.home.homepage.dao.prefecture.ZoneDao;
import com.home.homepage.service.prefecture.ZoneService;
import com.home.homepage.vo.prefecture.ZoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @author ：YQ袁（2446806940@qq.com）
 * @date ：Created in 2022/12/6 21:01
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ZoneServiceImpl implements ZoneService {
    @Autowired
    private ZoneDao zoneDao;
    @Override
    public Result<IPage<TbZone>> selectAll(Integer current, Integer pageSize, ZoneVo zoneVo) {
        IPage<TbZone> iPage = new Page<>(current,pageSize);
        QueryWrapper<TbZone> queryWrapper = new QueryWrapper<>();



        if (StringUtils.hasText(zoneVo.getZoneName())){
                queryWrapper.like("zone_name" , zoneVo.getZoneName());
        }
        if (StringUtils.hasText(zoneVo.getIsDisable())){
            queryWrapper.eq("is_disable",zoneVo.getIsDisable());
        }
        IPage<TbZone> iPage1 = zoneDao.selectPage(iPage, queryWrapper);
        return new Result<>(2000,"查看成功" ,iPage1);
    }

    @Override
    public Result updataStat(String id, Integer isDiable) {
        UpdateWrapper<TbZone> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("is_disable",isDiable);
        zoneDao.update(null,updateWrapper);
        return new Result<>(2000,"修改成功");
    }

    @Override
    public Result deleteZone(String id) {

        int i = zoneDao.deleteById(id);
        if (i>0){
            return new Result(2000,"删除成功");
        }
        return new Result(5000,"删除失败");
    }

    @Override
    public Result addZone(TbZone tbZone) {
        if (tbZone.getId() == null) {
            TbZone tbZone1 = new TbZone();
            tbZone1.setZoneName(tbZone.getZoneName());
            tbZone1.setZoneDesc(tbZone.getZoneDesc());
            tbZone1.setGmtCreate(LocalDateTime.now());
            tbZone1.setGmtModified(LocalDateTime.now());
            tbZone1.setIsDeleted("0");
            tbZone1.setIsDisable(0);
            int insert = zoneDao.insert(tbZone1);
            if (insert > 0) {
                return new Result(2000, "添加成功");
            }
        }
        if(tbZone.getId()!=null){
            UpdateWrapper<TbZone> wrapper = new UpdateWrapper<>();
            wrapper.eq("id" , tbZone.getId());
            wrapper.set("zone_name",tbZone.getZoneName());
            wrapper.set("zone_desc",tbZone.getZoneDesc());
            int update = zoneDao.update(null, wrapper);
            if (update>0) {
                return new Result(2000, "修改成功");
            }
        }
        return new Result(5000, "添加失败");
    }
}
